package com.gistmap.ecepoi.common;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.RemovalListener;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author zhangran
 * @date 2018/7/3
 */
@Slf4j
public enum ReflectCache {
    /**
     * single instance
     */
    INSTANCE;

    private static Cache<String,List<ExcelHeader>> cache;
    static {
        cache = CacheBuilder.newBuilder().maximumSize(100)
                .expireAfterWrite(24, TimeUnit.HOURS)
                .initialCapacity(10)
                .removalListener((RemovalListener<String, List<ExcelHeader>>) rn -> {
                    if(log.isInfoEnabled()){
                        log.info("被移除缓存{}:{}",rn.getKey(), rn.getValue());
                    }
                }).build();
    }


    public void put(String key,List<ExcelHeader> value){
        if(StringUtils.isNotEmpty(key) && value !=null){
            cache.put(key,value);
        }
    }

    public List<ExcelHeader> get(String key){
        return StringUtils.isNotEmpty(key) ? cache.getIfPresent(key) : null;
    }
}
